/*----------------------------------------------------------------------------------*/
/*!
 * @file leveltool.c
 * @brief kleines Test Programm für Level Files
 * @author steff
 *
 */
/*------------------------------------------------------------------------------------
------------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "level.hxx"
#include "yakwailog.hxx"

yakwailog logger;

int main(char arg_counter, char *arg_string[])
{
	struct sli_header header;
	logger.init("scro", NULL, DEFAULT_LOGLEVEL);
	if (arg_counter != 3)
	{
		printf("leveltool usage:\n");
		printf("leveltool [action] [filename]\n");
		printf("where action can be: --info or --generate\n");
		printf("leveltool (c)2007 steff, released under gpl\n");
	} else
	{
		
		if (strcmp("--info", arg_string[1]) == 0)
		{
			printf("leveltool: trying '%s'...\n", arg_string[2]);
		
			header = sli_load_header(arg_string[2]);
			if (header.version != 0)
			{
				printf("level format version: %d\n", header.version);
				printf("level author: %s\n", header.author);
				printf("level title: %s\n", header.title);
				printf("level tileset: %s\n", header.tileset);
				printf("level dimensions: %d x %d\n", header.dim_x, header.dim_y);
			} else
			{
				printf("FATAL: invalid file!\n");
			}
		} else if ((strcmp("--generate", arg_string[1]) == 0) || (strcmp("-g", arg_string[1]) == 0))
		{
			printf("leveltool: trying to generate '%s' with default vals...\n", arg_string[2]);
			if (sli_generate_level(arg_string[2], 25, 150) == 0)
			{
				printf("successfully finished.\n");
			} else
			{
				printf("error!\n");
			}
		} else if (strcmp("--show-body", arg_string[1]) == 0)
		{
			printf("leveltool: trying '%s'\n", arg_string[2]);
			header = sli_load_header(arg_string[2]);
			if (header.version != 0)
			{
				/*unsigned int * buffer;*/
//				buffer = (unsigned int *)calloc(header.dim_x*header.dim_y,sizeof(unsigned int));
//				if (buffer == NULL) printf("memory error: couldn't allocate memory\n!");
				printf("level format version: %d\n", header.version);
				printf("level author: %s\n", header.author);
				printf("level title: %s\n", header.title);
				printf("level tileset: %s\n", header.tileset);
				printf("level dimensions: %d x %d\n", header.dim_x, header.dim_y);
				printf("level data follows --------------------------------------->\n");
				sli_load_body(arg_string[2]/*, buffer*/);
				
				printf("level data end ------------------------------------------->\n");
				/*free(buffer);*/
			} else
			{
				printf("FATAL: invalid file!\n");
			}
		} else
		{
			printf("invalid action!\n");
		}
	}
	return 0;
}
